package ch.zbw.pra.sysventory.ui.gui.panels.settingspanel;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JLabel;
import javax.swing.JPanel;

import ch.zbw.pra.sysventory.domain.businesslogic.DomainController;
import ch.zbw.pra.sysventory.domain.visualisation.VisuControll;
import ch.zbw.pra.sysventory.ui.gui.panels.settingspanel.subpanels.ComputerSpezPanel;
import ch.zbw.pra.sysventory.ui.gui.panels.settingspanel.subpanels.NicSpezPanel;
import ch.zbw.pra.sysventory.ui.gui.panels.settingspanel.subpanels.OsSpezPanel;
import ch.zbw.pra.sysventory.ui.gui.panels.settingspanel.subpanels.PrinterSpezPanel;
import ch.zbw.pra.sysventory.ui.gui.panels.settingspanel.subpanels.ProcessorSpezPanel;
import ch.zbw.pra.sysventory.ui.gui.panels.settingspanel.subpanels.SelectButtonPanel;
import ch.zbw.pra.sysventory.ui.gui.panels.settingspanel.subpanels.SoftwareSpezPanel;
import ch.zbw.pra.sysventory.ui.gui.utilities.DefaultFonts.FontType;
import ch.zbw.pra.sysventory.ui.gui.utilities.GuiLayout;
import ch.zbw.pra.sysventory.ui.gui.utilities.TextLayout;

public class SettingsPanel extends JPanel {

	private static final long serialVersionUID = -9033998900901876155L;
	public static final int WIDTH_CHECKBOX = 250;
	private ComputerSpezPanel computerSpezPanel;
	private NicSpezPanel nicSpezPanel;
	private OsSpezPanel osSpezPanel;
	private PrinterSpezPanel printerSpezPanel;
	private ProcessorSpezPanel processorSpezPanel;
	private SoftwareSpezPanel softwareSpezPanel;
	private DomainController domainController;

	public SettingsPanel() {
		super();
		this.setBackground(Color.WHITE);
		VisuControll controller = VisuControll.getInstance();
		this.computerSpezPanel = new ComputerSpezPanel(controller.getComputer());
		this.nicSpezPanel = new NicSpezPanel(controller.getNic());
		this.osSpezPanel = new OsSpezPanel(controller.getOs());
		this.printerSpezPanel = new PrinterSpezPanel(controller.getPrinter());
		this.processorSpezPanel = new ProcessorSpezPanel(controller.getProcessor());
		this.softwareSpezPanel = new SoftwareSpezPanel(controller.getSoftware());

		this.domainController = DomainController.getInstance();
		this.domainController.receiveASettingsPanelReference(this);
		init();

	}

	public void init() {
		GridBagLayout gridBagLayout = GuiLayout.getGridBagLayout(this);
		gridBagLayout.columnWeights = new double[] { 1.0 };
		double noHeights = 0.0;
		double maxHeight = 1.0;
		gridBagLayout.rowWeights = new double[] { noHeights, noHeights, noHeights, noHeights, noHeights, noHeights, noHeights, noHeights, noHeights, noHeights, noHeights, noHeights, noHeights, noHeights, maxHeight };

		GridBagConstraints gridBagConstraints = GuiLayout.getGridBagConstraints();
		Insets titleInsets = new Insets(5, 5, 5, 5);
		int x = 0;
		int y = 0;
		gridBagConstraints.insets = titleInsets;
		JLabel settingsLabel = new JLabel("- Einstellungen zur Ansicht -");
		TextLayout.addStyleToComponent(settingsLabel, FontType.BIG);
		settingsLabel.setBackground(Color.ORANGE);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x, y, settingsLabel);
		y++;
		JLabel showInComputer = new JLabel("Computer-Ansicht:");
		TextLayout.addStyleToComponent(showInComputer, FontType.MEDIUM);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x, y, showInComputer);
		y++;
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x, y, this.computerSpezPanel);
		y++;
		JLabel showInNic = new JLabel("Netzwerk-Ansicht:");
		TextLayout.addStyleToComponent(showInNic, FontType.MEDIUM);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x, y, showInNic);
		y++;
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x, y, this.nicSpezPanel);
		y++;
		JLabel showInOs = new JLabel("Betriebsystem-Ansicht:");
		TextLayout.addStyleToComponent(showInOs, FontType.MEDIUM);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x, y, showInOs);
		y++;
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x, y, this.osSpezPanel);
		y++;
		JLabel showInPrinter = new JLabel("Drucker-Ansicht:");
		TextLayout.addStyleToComponent(showInPrinter, FontType.MEDIUM);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x, y, showInPrinter);
		y++;
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x, y, this.printerSpezPanel);
		y++;
		JLabel showInProcessor = new JLabel("Prozessor-Ansicht:");
		TextLayout.addStyleToComponent(showInProcessor, FontType.MEDIUM);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x, y, showInProcessor);
		y++;
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x, y, this.processorSpezPanel);
		y++;
		JLabel showInSoftware = new JLabel("Software-Ansicht:");
		TextLayout.addStyleToComponent(showInSoftware, FontType.MEDIUM);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x, y, showInSoftware);
		y++;
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x, y, this.softwareSpezPanel);
		y++;
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x, y, new SelectButtonPanel(this));
		y++;
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x, y, new JLabel());
	}

	public void setValueToAllCheckBoxes(boolean flag) {
		this.computerSpezPanel.setValueToAllCheckBoxes(flag);
		this.nicSpezPanel.setValueToAllCheckBoxes(flag);
		this.osSpezPanel.setValueToAllCheckBoxes(flag);
		this.printerSpezPanel.setValueToAllCheckBoxes(flag);
		this.processorSpezPanel.setValueToAllCheckBoxes(flag);
		this.softwareSpezPanel.setValueToAllCheckBoxes(flag);
	}

	public int countAllSelectedFlags() {

		int value = 0;
		if (this.computerSpezPanel.getHostNameState()) {
			value++;
		}
		if (this.computerSpezPanel.getSidState()) {
			value++;
		}
		if (this.computerSpezPanel.getComputerTypState()) {
			value++;
		}
		if (this.computerSpezPanel.getRamState()) {
			value++;
		}

		if (this.nicSpezPanel.getNicBezState()) {
			value++;
		}

		if (this.nicSpezPanel.getIpV4State()) {
			value++;
		}

		if (this.nicSpezPanel.getIpV6State()) {
			value++;
		}

		if (this.nicSpezPanel.getMacState()) {
			value++;
		}

		if (this.nicSpezPanel.getSubnetState()) {
			value++;
		}

		if (this.nicSpezPanel.getGatewayState()) {
			value++;
		}

		if (this.nicSpezPanel.getDhcpState()) {
			value++;
		}

		if (this.osSpezPanel.getOsNameState()) {
			value++;
		}

		if (this.osSpezPanel.getBuildNrState()) {
			value++;
		}

		if (this.osSpezPanel.getArchitekturState()) {
			value++;
		}

		if (this.printerSpezPanel.getPrinterNameState()) {
			value++;
		}

		if (this.printerSpezPanel.getPrinterDriverNameState()) {
			value++;
		}

		if (this.printerSpezPanel.getPrinterDriverVersionState()) {
			value++;
		}

		if (this.processorSpezPanel.getProzessorNameState()) {
			value++;
		}

		if (this.processorSpezPanel.getProzFamilyState()) {
			value++;
		}

		if (this.processorSpezPanel.getAnzKerneState()) {
			value++;
		}

		if (this.processorSpezPanel.getClockState()) {
			value++;
		}

		if (this.softwareSpezPanel.getSoftwareNameState()) {
			value++;
		}
		if (this.softwareSpezPanel.getSoftwareVendorState()) {
			value++;
		}

		if (this.softwareSpezPanel.getSoftwareVersionState()) {
			value++;
		}

		return value;
	}

	public void setAllEnabledStates(boolean beEnabledFlag) {
		this.computerSpezPanel.setAllEnabledStates(beEnabledFlag);
		this.nicSpezPanel.setAllEnabledStates(beEnabledFlag);
		this.osSpezPanel.setAllEnabledStates(beEnabledFlag);
		this.printerSpezPanel.setAllEnabledStates(beEnabledFlag);
		this.processorSpezPanel.setAllEnabledStates(beEnabledFlag);
		this.softwareSpezPanel.setAllEnabledStates(beEnabledFlag);

	}
}
